<script setup lang="ts">
import { reactive } from "vue";
import { useRouter } from "vue-router";
import HelloWorld from "@/components/HelloWorld.vue";

const state = reactive({
  showMenu: true,
  defaultOpen: ["1", "2", "3", "4"],
  currentPath: "/",
});

const noMenu = ["/login"];
const router = useRouter();
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    // 如果路径是 /login 则正常执行
    next();
  } else {
    // 如果不是 /login，判断是否有 token
    if (!localStorage.getItem("token")) {
      // 如果没有，则跳至登录页面
      next({ path: "/login" });
    } else {
      // 否则继续执行
      next();
    }
  }
  state.showMenu = !noMenu.includes(to.path);
  state.currentPath = to.path;
});
</script>

<template>
  <div class="layout">
    <el-container v-if="state.showMenu" class="container">
      <el-aside class="aside">
        <div class="head">
          <div>
            <img src="//s.weituibao.com/1582958061265/mlogo.png" alt="logo" />
            <span>vue3 admin</span>
          </div>
        </div>
        <div class="line" />
        <el-menu
          background-color="#222832"
          text-color="#fff"
          :router="true"
          :default-openeds="state.defaultOpen"
          :default-active="state.currentPath"
        >
          <el-menu-item index="/"
            ><el-icon><Odometer /></el-icon>首页</el-menu-item
          >
          <el-sub-menu index="1">
            <template #title>
              <span>组件</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/draggable"
                ><el-icon><Odometer /></el-icon>draggable</el-menu-item
              >
              <el-menu-item index="/elementTable"
              ><el-icon><Odometer /></el-icon>elementTable</el-menu-item
              >
            </el-menu-item-group>
          </el-sub-menu>
          <el-sub-menu index="2">
            <template #title>
              <span>DEMO</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/cesium"
                ><el-icon><Picture /></el-icon>CESIUM</el-menu-item
              >
              <el-menu-item index="/prismPlayer"
                ><el-icon><StarFilled /></el-icon>AliPlayer</el-menu-item
              >
              <el-menu-item index="/webSocket"
                ><el-icon><Sell /></el-icon>WEBSOCKET</el-menu-item
              >
              <el-menu-item index="/jsxView"
              ><el-icon><Sell /></el-icon>JSXComponent</el-menu-item
              >
              <el-menu-item index="/antvG6"
              ><el-icon><Sell /></el-icon>AntvG6</el-menu-item
              ><el-menu-item index="/antvS2"
              ><el-icon><Sell /></el-icon>AntvS2</el-menu-item
              >
            </el-menu-item-group>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-container class="content">
        <Header />
        <div class="main">
          <router-view />
        </div>
        <Footer />
      </el-container>
    </el-container>
    <el-container v-else class="loginContainer">
      <div class="main">
        <router-view />
      </div>
    </el-container>
  </div>
</template>

<style>
@import "@/assets/base.css";
</style>
<style scoped>
.layout {
  min-height: 100vh;
  background-color: #ffffff;
}
.container {
  height: 100vh;
}
.aside {
  width: 200px !important;
  background-color: #222832;
}
.head {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
}
.head > div {
  display: flex;
  align-items: center;
}

.head img {
  width: 50px;
  height: 50px;
  margin-right: 10px;
}
.head span {
  font-size: 20px;
  color: #ffffff;
}
.line {
  border-top: 1px solid hsla(0, 0%, 100%, 0.05);
  border-bottom: 1px solid rgba(0, 0, 0, 0.2);
}
.content {
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  overflow: hidden;
}
.main {
  height: calc(100vh - 100px);
  overflow: auto;
  padding: 10px;
}
.loginContainer {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
